﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Plugin.MyTemplate.Factory
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        const string key = "MyTemplate";


        char[] Replace(char[] inputdata, int inputlen)
        {
            for(int i=0; i<)
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<string> files = new List<string>()
            
            {"dllmain.cpp",
                "MainForm.cpp",
                "MainForm.h",                
                "Plugin.MyTemplate.rc",
                "PluginFactory.cpp",
                "PluginFactory.h",
                "Resource.h",
                "stdafx.cpp",
                "stdafx.h",
                "targetver.h",
                "Version.cpp",
                "Plugin.MyTemplate.vcproj"
            };

            string newtext = this.textBox1.Text;

            Directory.CreateDirectory(newtext);
            byte[] indata = new byte[1024*1024];
            char[] chardata = new char[1024 * 1024];
          //  byte[] outdata = new byte[1024 * 1024];


            foreach (var item in files)
            {
                //1.read data
                var orifname = "Plugin.MyTemplate\\" + item;                
                int len = File.Open(orifname, FileMode.Open).Read(indata, 0, 1024 * 1024);                
                Decoder d = Encoding.UTF8.GetDecoder();
                int chardatalen = d.GetChars(indata, 0, len, chardata, 0);


                //2. replace data
                int replen = 11;

                //3. write data
                byte[] outdata = new UTF8Encoding().GetBytes(chardata,0,replen);

                var newitem = item.Replace(key, newtext);
                var newfname = newtext + "\\" + newitem;

                FileStream fs = new FileStream(newfname, FileMode.Create);
                fs.Write(outdata,0,outdata.Length);
                fs.Flush();
                fs.Close();
              


            }
        }

    }
}
